home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / rbsetnv1.zip / SETENV.C < prev    next >
Text File  |  1991-01-07  |  3KB  |  103 lines

  1. /*
  2.  * setenv.c - set values in the parent environment
  3.  *
  4.  * Author:        R. Brittain                        4/11/90
  5.  *    Popen() is taken from code obtained from simtel20
  6.  *  Recommended wildcard expansion code to use with this is wildargv.c by
  7.  *     Frank Whaley and placed in the public domain
  8.  *  Environment location/manipulation borrowed from various places, but now
  9.  *  mangled beyond recognition
  10.  *
  11.  * Syntax:
  12.  *        setenv variable value......
  13.  *
  14.  * Description:
  15.  *  The command line is iteratively scanned for command substitutions in `...`
  16.  *  and environment variables (%var), and the resulting text used as the value
  17.  *  of the environment variable named in the first argument.
  18.  *  Command substitutions are performed by calling system(), which loads a
  19.  *  shell to run the the command.  If the first character of the command is '@'
  20.  *  the command is exec'ed directly (much faster than calling system, but
  21.  *  only works with .exe and .com files)
  22.  *
  23.  *  The magic characters %,`, and @ are set by #defines so you can change
  24.  *  them if you don't like them (comsub.h)
  25.  *
  26.  *                    This code placed in the public domain
  27.  *
  28.  *  Revision history
  29.  *    1.0        Dec 90 RB - first posted to usenet
  30.  *    1.01    Jan 91 RB - fixed argument parsing so `.....` can span multiple
  31.  *                arguments without needing to be double quoted
  32.  *                  - fixed location of environment for pre DOS 3.3 lowest level
  33.  *                  command.com
  34.  *
  35.  */
  36. #include <string.h>
  37. #include <ctype.h>
  38. #include <dos.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41.  
  42. #include "comsub.h"
  43.  
  44. /* prototypes */
  45.  
  46. int      get_env_index(char *var, char **env_var);
  47. int       getenvseg(unsigned *envseg);
  48. unsigned get_env_var(unsigned env_addr, char ***env_var, int *count);
  49. void      put_env_var(unsigned env_addr, unsigned seglen, char **env_var);
  50. void      fatal(char *msg, int status);
  51.  
  52.  
  53. main(int argc, char **argv)
  54. {
  55.     char version[] = "setenv version 1.01 of "__DATE__ ;
  56.     char usage[] = "Usage: setenv variable value\n";
  57.     unsigned env_addr, seglen;
  58.     int index, i, count;
  59.     char *value, **env_var;
  60.  
  61.     if (argc == 1)
  62.         fatal(usage,1);
  63.  
  64.     if ( !getenvseg(&env_addr))
  65.         fatal ("Cannot locate environment\n",2);
  66.  
  67.     /* process command line for back-quotes and unexpanded env. vars. */
  68.     rebuild_argv(&argc,&argv);
  69.  
  70.     /* convert variable to upper case for compatibility */
  71.     strupr(argv[1]);
  72.  
  73.     seglen = get_env_var( env_addr, &env_var, &count );
  74.     index  = get_env_index( argv[1], env_var );
  75.     if (index == count) env_var[index+1] = (char *)NULL;
  76.  
  77.     if (argc == 2) {
  78.         /* no value specified - take as a request to remove this entry */
  79.         value = "";
  80.     } else {
  81.         /* set the value of the variable to the rest of the arguments */
  82.         value = (char *) malloc(strlen(argv[1]) + 2);
  83.         strcpy(value,argv[1]);
  84.         strcat(value,"=");
  85.         for (i=2; i < argc; i++) {
  86.             value = (char *) realloc(value, strlen(value) + strlen(argv[i]) + 2);
  87.             strcat(value,argv[i]);
  88.             strcat(value," ");
  89.         }
  90.         *(endptr(value)-1) = '\0';
  91.     }
  92.     env_var[index] = value;
  93.     put_env_var(env_addr, seglen, env_var);
  94.     return(0);
  95. }
  96.  
  97.  
  98. void fatal(char *msg, int status)
  99. {
  100.     fputs(msg,stderr);
  101.     exit(status);
  102. }
  103.